package com.cuitrip.business.bill;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.business.bill.model.Bills;
import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.business.webview.proxy.WalletHelpDocProxy;
import com.cuitrip.business.webview.proxy.WebViewProxy;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.jumper.FragmentJumper;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rx.bus.RxBus;
import com.lab.utils.NumberUtils;
import com.lab.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by sheena on 16/4/27.
 */
public class WalletMainFragment extends CustomUiFragment implements IProxyCallback{

    public static final int DEFAULT_SIZE = 10;
    @Bind(R.id.amount)
    TextView amount;
    @Bind(R.id.money_type)
    TextView billMoneyType;
    @Bind(R.id.withdraw)
    ItemLayout withdrawLayout;
    @Bind(R.id.ratingToday)
    TextView mRatingToday;
    private ApiProxy apiProxy=new ApiProxy(this);
    private int index = 0;
    private Bills bills;
    private Subscription subscription;

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.wallet);
        config.mRightStyle = CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarRightAreaName=getString(R.string.query_icon);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState,R.layout.bill);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this,getView());
        subscription = RxBus.getDefault().toObservable().subscribe(new Action1<Object>() {
            @Override
            public void call(Object o) {
                if (o instanceof WithdrawEvent) {
                    OrderBusiness.getBills(apiProxy, index * DEFAULT_SIZE, DEFAULT_SIZE);
                }

            }
        });
    }

    @Override
    public void initData() {
        OrderBusiness.getBills(apiProxy, index * DEFAULT_SIZE, DEFAULT_SIZE);
        showNoCancelDialog();
    }

    @OnClick(R.id.withdraw)
    public void withdraw(){
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(),R.id.container,WithdrawMoneyFragment.newInstance(bills),WithdrawMoneyFragment.class.getName(),true);
    }

    @OnClick(R.id.accounts)
    public void jump2Accounts(){
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(),R.id.container,AccountsFragment.newInstance(),AccountsFragment.class.getName(),true);
    }

    @OnClick(R.id.transaction)
    public void jump2Transaction(){
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(),R.id.container,TransactionFragment.newInstance(),TransactionFragment.class.getName(),true);
    }

    @OnClick(R.id.ratingToday)
    public void showRating(){
        ExplainActivity.startActivityBy(getHostActivity(), ExplainActivity.SHOW_INFO_ABOUT_RATING);
    }


    @Override
    public void onTopbarLeftPress() {
        getHostActivity().finish();
    }

    @Override
    public void onTopbarRightPress() {
        WebViewProxy.getInstance().browseWeb(getHostActivity(), WalletHelpDocProxy.formatUrl(H5UrlMaker.getWalletBalanceHelpUrl()),false);
    }


    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()) {
            return false;
        }
            CtApiResponse response = (CtApiResponse) result.getData();
            if (response.isResponseNormal()) {
                if (response.result instanceof Bills) {
                    bills = (Bills) response.result;
                    amount.setText("$ " + NumberUtils.getMoneyFormat(bills.balance));
                    billMoneyType.setText("(" + bills.moneyType.toUpperCase() + ")");
                    hideNoCancelDialog();
                    return false;
                }
            } else {
                hideNoCancelDialog();
                ToastUtil.toast(getContext(), response.msg, 0);
                return false;
            }
            hideNoCancelDialog();
            return false;
        }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!subscription.isUnsubscribed()){
            subscription.unsubscribe();
        }
    }
}
